﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Sync;

namespace TaskSync2010
{
    public partial class SettingsForm : Form
    {
        public SettingsForm()
        {
            InitializeComponent();
            tbUser.Text = UserSettings.Username;
            tbPassword.Text = UserSettings.Password;
            tbProxyUser.Text = UserSettings.ProxyUsername;
            tbProxyPassword.Text = UserSettings.ProxyPassword;
            tbProxy.Text = UserSettings.ProxyServer;
            syncInterval.Text = UserSettings.Interval.ToString();
        }

        private void tbUser_Validating(object sender, CancelEventArgs e)
        {
            if(tbPassword.Text!="") ValidateAccountData();
        }

        private void ValidateAccountData()
        {
            ThisAddIn.Syncer=new Syncer(tbUser.Text,tbPassword.Text);
            if (ThisAddIn.Syncer.Ok)
            {
                lbVerified.Text = "Verified";
                lbVerified.ForeColor = Color.Green;
                UserSettings.Username = tbUser.Text;
                UserSettings.Password = tbPassword.Text;
                UserSettings.Save();
            }
            else
            {
                lbVerified.Text = "Incorrect";
                lbVerified.ForeColor = Color.Red;
            }                
        }

        private void tbPassword_Validating(object sender, CancelEventArgs e)
        {
            if (tbUser.Text != "") ValidateAccountData();
        }

        private void cbProxy_CheckedChanged(object sender, EventArgs e)
        {
            groupBox1.Enabled = cbProxy.Checked;
            UserSettings.ProxyInUse = cbProxy.Checked;          
        }

        private void SettingsForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            UserSettings.ProxyPassword = tbProxyPassword.Text;
            UserSettings.ProxyUsername = tbProxyUser.Text;
            UserSettings.ProxyServer = tbProxy.Text;
            UserSettings.ProxyPort = 80; //todo add link
        }

     
       
    }
}
